#include "WindowsBase.h"

using namespace acme::windows;

bool Process::Exec(LPTSTR pathName)
{
  STARTUPINFO si = {0};
  si.cb = sizeof si;
  GetStartupInfo(&si);
  PROCESS_INFORMATION pi = {0};
  BOOL ret = CreateProcess(NULL,pathName,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
  return ret!=0;
}

int Process::System(LPTSTR pathName)
{
  if(!pathName)
    return ERROR_PATH_NOT_FOUND;
  STARTUPINFO si = {0};
  si.cb = sizeof si;
  GetStartupInfo(&si);
  PROCESS_INFORMATION pi = {0};
  BOOL retCP = CreateProcess(NULL,pathName,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
  if(!retCP)
    throw Error();
  CloseHandle(pi.hThread);
  DWORD retWait = WaitForSingleObject(pi.hProcess,INFINITE);
  if(retWait<0)
    throw Error();
  DWORD retCode;
  if(!GetExitCodeProcess(pi.hProcess,&retCode))
    throw Error();
    // something interesting happened
  CloseHandle(pi.hProcess);
  return retCode;
}